package com.cs.controller;

import com.cs.pojo.Role;
import com.cs.pojo.User;
import com.cs.resultVO.ResultVO;
import com.cs.service.RoleService;
import com.cs.service.UserService;
import com.cs.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("")
@ResponseBody
public class LoginController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @RequestMapping("loginCode")
    public ResultVO<User> loginCode(HttpServletRequest request) {
        UserUtil.setCode(request.getSession() , request.getParameter("code"));
        return new ResultVO<>();
    }
    
    @RequestMapping (value = "logIn", method = RequestMethod.POST)
    public ResultVO<User> login(HttpServletRequest request , User user) {
        /* 用户登录成功条件
         *   1. 验证码 code 验证
         *   2. 账号是否存在
         *   3. 且角色是否匹配
         *   4. 账号是否启用
         *   5. 密码是否匹配
         * */
        
        System.out.println("=======");
        System.out.println("Code : " + UserUtil.getCode(request.getSession()));
        System.out.println("user.toString() : " + user.toString());
        
        String reCode = request.getParameter("recode");
        String code = UserUtil.getCode(request.getSession());
        User userTmp = null;
        
        // 1. 验证码 code 验证
        if (!reCode.equals(code)){
            return new ResultVO<>(500 , "验证码不匹配");
        }
        // 2. 账号是否存在
        for (Role role : roleService.queryAll()) {
            List<User> users = userService.queryLPR(user.getuLoginname() , role.getrId());
            if (!users.isEmpty()){
                userTmp = users.get(0);
                break;
            }
        }
        if (userTmp == null){
            return new ResultVO<>(500,"不存在该用户！");
        }
        // 3. 角色是否匹配
        if (!user.getrId().equals(userTmp.getrId())){
            return new ResultVO<>(500 , "角色不匹配");
        }
        // 4. 账号是否启用
        if (userTmp.getuState() != 0){
            return new ResultVO<>(500,"该用户已被 禁用 或 删除！");
        }
        // 5. 密码是否匹配
        String oldPass = userTmp.getuPassword();
        String newPass = user.getuPassword();
        if (!oldPass.equals(newPass)){
            return new ResultVO<>(500,"密码有误！");
        }
        //登录成功 会话信息记录
        UserUtil.setUserId(request.getSession() , userTmp.getuId());
        return new ResultVO<>();
    }

    @RequestMapping(value = "loadInfo" , method = RequestMethod.GET)
    public ResultVO<User> getInfo(HttpServletRequest request) {
        int userId = UserUtil.getUserId(request.getSession());
        System.out.println("userId : " + userId);
        User user = userService.queryById(userId);
        return new ResultVO<>(user);
    }

    @RequestMapping("exit.do")
    public ResultVO exit(HttpServletRequest request) {
        request.getSession().invalidate();
        return new ResultVO();
    }


}
